from pydantic import BaseModel, Field
from pydantic_settings import BaseSettings
from typing import List
import yaml


class StockConfig(BaseModel):
    name: str
    alias: str
    feeds_aliases: List[str]


class CeleryConfig(BaseModel):
    worker_hijack_root_logger: bool = True
    broker_connection_retry_on_startup: bool = False
    worker_concurrency: int = 1


class FNSAInferenceManagerConfig(BaseModel):
    log_level: str
    jobs_interval_seconds: int
    stocks: List[StockConfig]
    celery: CeleryConfig
    analyze_sentiment_uri: str
    telegram_bot_uri: str  # Add the telegram bot URI field


class AppConfig(BaseSettings):
    config_file_path: str = Field(..., env='CONFIG_FILE_PATH')
    etcd_host: str = Field(..., env='ETCD_HOST')
    etcd_port: int = Field(..., env='ETCD_PORT')
    redis_host: str = Field(..., env='REDIS_HOST')
    redis_port: int = Field(..., env='REDIS_PORT')
    redis_db: int = Field(..., env='REDIS_DB')
    
    class Config:
        env_file = '.env'


def load_config():
    app_config = AppConfig()
    
    with open(app_config.config_file_path, 'r') as file:
        config_dict = yaml.safe_load(file)
    
    fnsa_inference_config_dict = config_dict.get('fnsa-inference-manager')
    
    if fnsa_inference_config_dict is None:
        raise ValueError("fnsa-inference-manager configuration is missing")
    
    fnsa_inference_config = FNSAInferenceManagerConfig(**fnsa_inference_config_dict)
    
    return fnsa_inference_config, app_config.etcd_host, app_config.etcd_port, app_config.redis_host, app_config.redis_port, app_config.redis_db
